from django.db import models
from base_configs.models import EmbeddingType


class Project(models.Model):
    name = models.CharField(max_length=100, verbose_name='项目名称')
    status = models.PositiveSmallIntegerField(choices=((1, "有效"), (0, "无效")), editable=False, default=1,
                                              verbose_name="状态")
    comment = models.CharField(max_length=100, default=None, blank=True, null=True, verbose_name="项目描述")
    create_time = models.DateTimeField(verbose_name="创建时期", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

    def __str__(self):
        return '{}'.format(self.name)

    class Meta:
        verbose_name = "项目库"
        verbose_name_plural = verbose_name


class Base(models.Model):
    name = models.CharField(max_length=100, verbose_name='知识库名称')
    category = models.CharField(max_length=100, verbose_name='知识库类型')
    # embedding = models.ForeignKey(Config, on_delete=models.CASCADE, verbose_name='嵌入模型')
    group_name = models.CharField(max_length=100, blank=True, null=True, verbose_name='分组名称')

    status = models.PositiveSmallIntegerField(choices=((1, "有效"), (0, "无效")), editable=False, default=1,
                                              verbose_name="状态")
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')

    comment = models.CharField(max_length=100, default=None, blank=True, null=True, verbose_name="备注")
    create_time = models.DateTimeField(verbose_name="创建时期", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

    def __str__(self):
        return f'{self.name}'

    class Meta:
        verbose_name = "知识库"
        verbose_name_plural = verbose_name


class Source(models.Model):
    uid = models.CharField(max_length=100, blank=True, null=True, verbose_name='唯一id')
    name = models.CharField(max_length=100, verbose_name='来源')
    # project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    # label = models.ForeignKey(Label, on_delete=models.CASCADE, verbose_name='标签')
    status = models.PositiveSmallIntegerField(choices=((1, "有效"), (0, "无效")), editable=False, default=1,
                                              verbose_name="状态")
    comment = models.CharField(max_length=100, default=None, blank=True, null=True, verbose_name="备注")
    create_time = models.DateTimeField(verbose_name="创建时期", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

    def __str__(self):
        return '{}'.format(self.name)

    class Meta:
        verbose_name = "来源库"
        verbose_name_plural = verbose_name


class Label(models.Model):
    name = models.CharField(max_length=100, verbose_name='标签名称')
    source = models.ForeignKey(Source, on_delete=models.CASCADE, verbose_name='来源')
    status = models.PositiveSmallIntegerField(choices=((1, "有效"), (0, "无效")), editable=False, default=1,
                                              verbose_name="状态")
    comment = models.CharField(max_length=100, default=None, blank=True, null=True, verbose_name="备注")
    create_time = models.DateTimeField(verbose_name="创建时期", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

    def __str__(self):
        return '{}'.format(self.name)

    class Meta:
        verbose_name = "标签库"
        verbose_name_plural = verbose_name


project_list = Project.objects.all()


class Detail(models.Model):
    uid = models.CharField(max_length=64, unique=True, db_index=True, verbose_name='唯一id')
    # base = models.ForeignKey(Base, on_delete=models.CASCADE, verbose_name='知识库名称')
    base = models.ForeignKey(Base, on_delete=models.SET_DEFAULT, default=-1, verbose_name='知识库名称')

    text = models.TextField(verbose_name='知识内容')
    detail_text = models.TextField(blank=True, null=True, verbose_name='额外知识内容')
    # source = models.ForeignKey(Source, on_delete=models.CASCADE, verbose_name='来源')
    source = models.ForeignKey(Source, on_delete=models.SET_DEFAULT, default=-1, verbose_name='来源')
    label = models.CharField(max_length=256, verbose_name='标签', null=True, blank=True)
    # embedding = models.ForeignKey(Config, on_delete=models.CASCADE, verbose_name='嵌入模型')
    embedding = models.ForeignKey(EmbeddingType, on_delete=models.SET_DEFAULT, default=-1, verbose_name='嵌入模型')
    # embedding = models.CharField(max_length=64, verbose_name='嵌入模型')
    parent_id = models.CharField(max_length=128, null=True, verbose_name='主问题id')
    # 记得json压缩
    meta = models.JSONField(max_length=256,  verbose_name='备注信息', default={"info": ""})

    status = models.PositiveSmallIntegerField(choices=((1, "有效"), (0, "无效")), editable=False, default=1,
                                              verbose_name="状态")
    comment = models.CharField(max_length=100, default=None, blank=True, null=True, verbose_name="备注")
    create_time = models.DateTimeField(verbose_name="创建时期", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

    def __str__(self):
        return '{}'.format(self.text)

    def save_origin(self, *args, **kwargs):
        super().save(*args, **kwargs)
        print("通过接口写入数据！")

    class Meta:
        verbose_name = "详细知识"
        verbose_name_plural = verbose_name
        unique_together = ()


class DataSynchronizationError(models.Model):
    data_json = models.TextField(verbose_name='传参数据')
    error = models.CharField(max_length=256, verbose_name='错误', null=True, blank=True)
    status = models.PositiveSmallIntegerField(choices=((1, "数据已处理"), (0, "数据未处理")), editable=False, default=1,
                                              verbose_name="状态")
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    def __str__(self):
        return '{}'.format(self.data_json)

    class Meta:
        verbose_name = "数据同步失败信息"
        verbose_name_plural = verbose_name
